/************************************************************************
                        ReplyData.h.cpp - Copyright asaf
*/
#include "ReplyData.h"
#include "..\Shered\GlobalFunctions.h"
#include <iomanip>
#include <algorithm>
using namespace std;
// Constructors/Destructors
ReplyData::ReplyData (int type):Reply(type),m_rowCount(0){;}
ReplyData::ReplyData(int errNumber,string errDescription):Reply(errNumber),m_rowCount(0)
{
	addParameter("Error Description",errDescription);
}
ReplyData::~ReplyData ( ) { }

string ReplyData::getParameter(string column,unsigned int row)const
{
	Recordset::const_iterator iter;
	iter=m_Recordset.find(column);
	if(iter==m_Recordset.end()||iter->second.data.size()-1<row)return "";
	return iter->second.data[row];
}
string ReplyData::getParameter(unsigned int column,unsigned int row)const
{
	Recordset::const_iterator iter;
	if (column>m_columnheaders.size())return "";
	iter=m_Recordset.find(m_columnheaders[column]);
	if(iter==m_Recordset.end()||iter->second.data.size()-1<row)return "";
	return iter->second.data[row];
}
void ReplyData::addParameter(string column,string data)
{
	Recordset::const_iterator iter;
	
	iter=m_Recordset.find(column);
	if(iter==m_Recordset.end()){
		m_Recordset[column].maxStringLen=0;
		m_columnheaders.push_back(column);
	}
	if((iter!=m_Recordset.end()&&
		(iter->second.data.size()>=m_rowCount))||(m_rowCount==0))m_rowCount++;
	try{
		m_Recordset[column].data.push_back(data);
	}catch(...)
	{
		m_Recordset[column].data.pop_back();
		throw;
	}
	if(data.length()>m_Recordset[column].maxStringLen)
		m_Recordset[column].maxStringLen=data.length();
}

unsigned int ReplyData::getMaxStringLen(string column)
{
	Recordset::const_iterator iter;
	iter=m_Recordset.find(column);
	if(iter==m_Recordset.end())return 0;
	return m_Recordset[column].maxStringLen;
}
string ReplyData::getErrDescription()
{
	return getParameter("Error Description",0);
}
void ReplyData::print_recordset()
{
/*	Recordset::iterator r_iter,r2_iter;
	for(r_iter=m_Recordset.begin();r_iter!=m_Recordset.end();r_iter++)
		cout<<setw(max(r_iter->first.length(),r_iter->second.maxStringLen))<<r_iter->first<<"|";
		cout<<endl;
	for(int cur_row=0;cur_row<m_rowCount;cur_row++)
	{
		for(r_iter=m_Recordset.begin();r_iter!=m_Recordset.end();r_iter++)
			cout<<setw(max(r_iter->first.length(),r_iter->second.maxStringLen))<<getParameter(r_iter->first,cur_row)<<"|";
		cout<<endl;
	}
	*/
	for(int cur_col=0;cur_col<m_columnheaders.size();cur_col++)
		cout<<setw(max(m_columnheaders[cur_col].length(),m_Recordset[m_columnheaders[cur_col]].maxStringLen))<<m_columnheaders[cur_col]<<"|";
	cout<<endl;
	for(int cur_row=0;cur_row<m_rowCount;cur_row++)
	{
		for(int cur_col=0;cur_col<m_columnheaders.size();cur_col++)
			cout<<setw(max(m_columnheaders[cur_col].length(),m_Recordset[m_columnheaders[cur_col]].maxStringLen))<<getParameter(cur_col,cur_row)<<"|";
		cout<<endl;
	}

}

  